package com.zhuangfei.hpuhelper.view;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import cn.lzf.serviceplatform.R;

import com.hpu.bean.Info;
import com.hpu.bean.Version;
import com.hpu.constants.HttpConstants;
import com.hpu.constants.ShareConstants;
import com.hpu.parser.InfoParser;
import com.hpu.parser.VersionParser;
import com.hpu.service.InfoService;
import com.hpu.service.LoginService;
import com.hpu.service.UploadHeadImageService;
import com.hpu.tools.ShareTools;
import com.zhuangfei.generalpage.BaseActivity;
import com.zhuangfei.generalpage.ViewHelper;
import com.zhuangfei.hpuhelper.tools.DefaultHandler;
import com.zhuangfei.toolkit.tools.ActivityTools;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 个人中心
 * 
 * @author Administrator
 * 
 */
public class PersonActivity extends BaseActivity{

	private TextView nameTextView;
	private TextView sexTextView;
	private TextView xueyuanTextView;
	private TextView zhuanyeTextView;
	private TextView classTextView;
	private TextView kaohaoTextView;

	private LinearLayout backLayout;

	LoginService loginService;
	InfoService infoService;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_person);
		ActivityTools.setTransparent(this);
		initView();
		initEvent();
		checkData();
	}

	private void checkData() {
		String name=ShareTools.get(this, ShareConstants.STORE_USER_NAME, "default").toString();
		if(name.equals("default")){
			getViewHelper().showLoadingView();
			infoService.getInfo();
		}
		else{
			getViewHelper().showSuccessView();
			loadData();
		}
	}

	private void initView() {
		loginService = new LoginService(this,handler);
		infoService = new InfoService(handler, this);

		nameTextView = (TextView) findViewById(R.id.id_person_name);
		kaohaoTextView = (TextView) findViewById(R.id.id_person_kaohao);
		sexTextView = (TextView) findViewById(R.id.id_person_sex);
		xueyuanTextView = (TextView) findViewById(R.id.id_person_xueyuan);
		zhuanyeTextView = (TextView) findViewById(R.id.id_person_zhuanye);
		classTextView = (TextView) findViewById(R.id.id_person_class);
		backLayout = (LinearLayout) findViewById(R.id.id_back);
	}
	
	public void initEvent() {
		backLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				ActivityTools.toBackActivityAnim(PersonActivity.this, MenuActivity.class);
			}
		});
	}

	@SuppressLint("HandlerLeak")
	DefaultHandler handler = new DefaultHandler(this) {
		@Override
		public void handleMessage(Message msg) {
			getViewHelper().showSuccessView();
			super.handleMessage(msg);

			switch (msg.what) {
				// 登录成功
				case HttpConstants.LOGIN_SUCCESS:
					getViewHelper().showLoadingView();
					ShareTools.put(PersonActivity.this,ShareConstants.STORE_LOGINCOOKIE, msg.obj.toString());
					infoService.getInfo();
					break;

				// 获取个人数据成功
				case HttpConstants.QUERY_INFO_SUCCESS:
					InfoParser infoParser=new InfoParser();
					Info info=infoParser.parse(msg.obj.toString());
					saveInfo(info);
					loadData();
					break;

				case HttpConstants.VPN_TOOLONG:
					if(handler.isIgnoreVpnExpired()){
						break;
					}
					getViewHelper().showLoadingView();
					loginService.login(ShareTools.get(PersonActivity.this, ShareConstants.STORE_USER, "").toString(),
							ShareTools.get(PersonActivity.this, ShareConstants.STORE_PASSWORD1, "").toString(),
							ShareTools.get(PersonActivity.this, ShareConstants.STORE_PASSWORD2, "").toString());
					break;

				default:
					break;
			}
		};
	};


	/**
	 * 保存用户信息
	 *
	 */
	public void saveInfo(Info info) {
		if (info==null) return;
		System.out.println(info.getName());
		ShareTools.put(this,ShareConstants.STORE_USER_MAJOR, info.getMajor());
		ShareTools.put(this, ShareConstants.STORE_USER_INSTITUTE, info.getInstitute());
		ShareTools.put(this, ShareConstants.STORE_USER_TESTNUMBER, info.getTestNumber());
		ShareTools.put(this, ShareConstants.STORE_USER_CLASS, info.getClasses());
		ShareTools.put(this, ShareConstants.STORE_USER_SEX, info.getSex());
		ShareTools.put(this, ShareConstants.STORE_USER_NAME, info.getName());
	}

	public void loadData() {
		nameTextView.setText(ShareTools.get(this, ShareConstants.STORE_USER_NAME, "姓名未知").toString());
		sexTextView.setText(ShareTools.get(this, ShareConstants.STORE_USER_SEX, "性别未知").toString());
		xueyuanTextView.setText(ShareTools.get(this, ShareConstants.STORE_USER_INSTITUTE, "学院未知").toString());
		zhuanyeTextView.setText(ShareTools.get(this, ShareConstants.STORE_USER_MAJOR, "专业未知").toString());
		classTextView.setText(ShareTools.get(this, ShareConstants.STORE_USER_CLASS, "班级未知").toString());
		kaohaoTextView.setText(ShareTools.get(this, ShareConstants.STORE_USER_TESTNUMBER, "考生号未知").toString());
	}

	@Override
	public void onBackPressed() {
		ActivityTools.toBackActivityAnim(this, MenuActivity.class);
	}
	
}
